package com.csair.seam.domain.service.impl;

import java.text.NumberFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.csair.seam.domain.mapper.AssetsChangeMapper;
import com.csair.seam.domain.model.entity.AssetsChange;
import com.csair.seam.domain.model.param.EquipParam;
import com.csair.seam.domain.model.vo.AssetsChangeVo;
import com.csair.seam.domain.service.IAssetsChangeService;
@Service
public class AssetsChangeServiceImp implements IAssetsChangeService {
	
	@Autowired
	private AssetsChangeMapper assetsChangeMapper;

	//@DS("nc63")
	@Override
	public List<AssetsChangeVo> findAssetsChange(EquipParam param) {
		Map<String, AssetsChangeVo> map = new HashMap<>();
		
		List<AssetsChange> alterList = assetsChangeMapper.findAlter(param);
		
		for (AssetsChange assetsChange : alterList) {
			AssetsChangeVo vo = null;
			if(map.containsKey(assetsChange.getOrgCode())) {
				vo = map.get(assetsChange.getOrgCode());
			}else {
				vo = new AssetsChangeVo();
			}
			vo.setOrgName(assetsChange.getOrgName());
			if("4A07-02".equals(assetsChange.getTransiType())) {//使用部门变动
				vo.setUseDeptChangeUnfinishedNum(assetsChange.getUnfinishedNum());
				vo.setUseDeptChangeFinishedNum(assetsChange.getFinishedNum());
				vo.setUseDeptChangeTotalNum(assetsChange.getTotalNum());
				vo.setUseDeptChangeFinishedRate(assetsChange.getFinishedRate());
			}else if("4A07-03".equals(assetsChange.getTransiType())) {//责任人变动
				vo.setUserChangeUnfinishedNum(assetsChange.getUnfinishedNum());
				vo.setUserChangeFinishedNum(assetsChange.getFinishedNum());
				vo.setUserChangeTotalNum(assetsChange.getTotalNum());
				vo.setUserChangeFinishedRate(assetsChange.getFinishedRate());
			}else {//位置变动
				vo.setLocationChangeUnfinishedNum(assetsChange.getUnfinishedNum());
				vo.setLocationChangeFinishedNum(assetsChange.getFinishedNum());
				vo.setLocationChangeTotalNum(assetsChange.getTotalNum());
				vo.setLocationChangeFinishedRate(assetsChange.getFinishedRate());
			}
			if(!map.containsKey(assetsChange.getOrgCode())) {
				map.put(assetsChange.getOrgCode(), vo);
			}
		}
		
		List<AssetsChange> reduceList = assetsChangeMapper.findReduce(param);
		for (AssetsChange assetsChange : reduceList) {
			AssetsChangeVo vo = null;
			if(map.containsKey(assetsChange.getOrgCode())) {
				vo = map.get(assetsChange.getOrgCode());
			}else {
				vo = new AssetsChangeVo();
			}
			vo.setOrgName(assetsChange.getOrgName());
			vo.setReduceUnfinishedNum(assetsChange.getUnfinishedNum());
			vo.setReduceFinishedNum(assetsChange.getFinishedNum());
			vo.setReduceTotalNum(assetsChange.getTotalNum());
			vo.setReduceFinishedRate(assetsChange.getFinishedRate());
			if(!map.containsKey(assetsChange.getOrgCode())) {
				map.put(assetsChange.getOrgCode(), vo);
			}
		}
		List<AssetsChange> redeployOutList = assetsChangeMapper.findRedeployOut(param);
		for (AssetsChange assetsChange : redeployOutList) {
			AssetsChangeVo vo = null;
			if(map.containsKey(assetsChange.getOrgCode())) {
				vo = map.get(assetsChange.getOrgCode());
			}else {
				vo = new AssetsChangeVo();
			}
			vo.setOrgName(assetsChange.getOrgName());
			vo.setReDeployOutUnfinishedNum(assetsChange.getUnfinishedNum());
			vo.setReDeployOutFinishedNum(assetsChange.getFinishedNum());
			vo.setReDeployOutTotalNum(assetsChange.getTotalNum());
			vo.setReDeployOutFinishedRate(assetsChange.getFinishedRate());
			if(!map.containsKey(assetsChange.getOrgCode())) {
				map.put(assetsChange.getOrgCode(), vo);
			}
		}
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setMaximumFractionDigits(1);
		for (AssetsChangeVo vo : map.values()) {
			int totalFinishedNum = vo.getLocationChangeFinishedNum().intValue()
					+vo.getUserChangeFinishedNum().intValue()
					+vo.getUseDeptChangeFinishedNum().intValue()
					+vo.getReduceFinishedNum().intValue()
					+vo.getReDeployOutFinishedNum().intValue();
			int totalNum = vo.getLocationChangeTotalNum().intValue()
					+vo.getUserChangeTotalNum().intValue()
					+vo.getUseDeptChangeTotalNum().intValue()
					+vo.getReduceTotalNum().intValue()
					+vo.getReDeployOutTotalNum().intValue();
			if(totalNum > 0) {
				vo.setTotalNum(totalNum);
				vo.setAvgFinishedRate(numberFormat.format((float)totalFinishedNum/(float)totalNum*100));
			}
		}
		
		//按总单据量降序排序
		if(!map.isEmpty()) {
			return map.values().stream().sorted(Comparator.comparing(AssetsChangeVo::getTotalNum).reversed()).collect(Collectors.toList());
		}
		return Collections.emptyList();
	}

}
